﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class NavController : AdminBaseController
    {
        navCore core = new navCore();
        public ActionResult Get(int id)
        {
            if (!HasQx("nav"))
            {
                return Error("没有权限！");
            }
            return Success(core.GetOne(id));
        }
        public ActionResult GetList(int type=0)
        {
            if (!HasQx("nav"))
            {
                return Error("没有权限！");
            }
            var list = core.GetTree(type);
            return Success(list);
        }
        [HttpPost]
        public ActionResult Del(int id)
        {
            if (!HasQx("nav"))
            {
                return Error("没有权限！");
            }
            if (id > 0)
            {
                if (core.Del(id))
                {
                    Log(curmanage.username + "删除导航ID=" + id);
                    return Success("删除成功！");
                }

            }
            return Error("删除失败！");
        }
        [HttpPost]
        public ActionResult DelAll(List<navEntity> modelList)
        {
            if (!HasQx("nav"))
            {
                return Error("没有权限！");
            }
            string ids = string.Empty;
            foreach (var navEntity in modelList)
            {
                core.Del(navEntity);
                ids += navEntity.ID + ",";
            }
            Log(curmanage.username + "删除导航ID in (" + ids + ")");
            return Success("删除成功！");
        }
        [HttpPost]
        public ActionResult SaveOrdnum(List<navEntity> navlist)
        {
            if (!HasQx("nav"))
            {
                return Error("没有权限！");
            }
            foreach (var navEntity in navlist)
            {
                core.Update(new {navEntity.ordnum}, navEntity.ID);
            }
            return Success();
        }
        [HttpPost]
        public ActionResult Save(navEntity nav)
        {
            if (!HasQx("nav"))
            {
                return Error("没有权限！");
            }
            if (nav.title== null)
            {
                return Error();
            }

            if (nav.ID == 0)
            {
                nav.sid = 1;//TODO 多站点调整
                nav.mid = curmanage.ID;
            }
            var model= core.Save(nav);
            Log(curmanage.username + "保存导航" + model.ToJson());
            return Success(model);
        }
    }
}
